package com.example.test;

import org.apache.commons.io.FileUtils;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.*;

public class Demo03 {
    public static Map<String, String> map=new HashMap<>();
    public static String ui="sh08-bond-ui";
    public static String business="bond-business-hsbc";
    public static String dir="C:\\Users\\lei.liu\\Desktop\\hsbc\\下载\\";
//    public static String dir="/home/liulei/Downloads/";
    public static String uri="http://maven.kingstarfintech.com/repository/KSP/com/kingstar/ceres/";
//    public static String uri="http://maven.kingstarfintech.com/repository/BNK/com/kingstar/ceres/";
//    public static List<String> list = Arrays.asList("sh08-bond-service","bond-business-hsbc","sh08-bond-ui","sh08-bis-service","sh08-risk-service","sh08-mds-service");
//    public static List<String> list = Arrays.asList("sh08-bond-service","bond-business-hsbc","sh08-bis-service","sh08-risk-service","sh08-mds-service");
    public static List<String> list = Arrays.asList("bond-business-hsbc","sh08-bond-service");
    public static String version="2.0.8B48";
//    public static String dir="/home/liulei/Downloads/";
    static {
        map.put("url","%ssh08/%s/%s/%s");
        map.put("buUrl","%s%s/%s/%s");
        map.put("fileName","%s-%s-assembly-release.tar.gz");
        map.put("uiName","%s-%s.tar.gz");
        map.put("buName","%s-%s.jar");
//        map.put("bond-business-hsbc-2.0.8B47.jar","bond-business-hsbc/2.0.8B47/bond-business-hsbc-2.0.8B47.jar");
//        map.put("sh08-bond-ui-2.0.8B47.tar.gz","http://maven.kingstarfintech.com/repository/BNK/com/kingstar/ceres/sh08/sh08-bond-ui/2.0.8B47/sh08-bond-ui-2.0.8B47.tar.gz");
//        map.put("sh08-bis-service-2.0.8B47-assembly-release.tar.gz","http://maven.kingstarfintech.com/repository/BNK/com/kingstar/ceres/sh08/sh08-bis-service/2.0.8B47/sh08-bis-service-2.0.8B47-assembly-release.tar.gz");
//        map.put("sh08-risk-service-2.0.8B47-assembly-release.tar.gz","http://maven.kingstarfintech.com/repository/BNK/com/kingstar/ceres/sh08/sh08-risk-service/2.0.8B47/sh08-risk-service-2.0.8B47-assembly-release.tar.gz");
//        map.put("sh08-mds-service-2.0.8B47-assembly-release.tar.gz","http://maven.kingstarfintech.com/repository/BNK/com/kingstar/ceres/sh08/sh08-mds-service/2.0.8B47/sh08-mds-service-2.0.8B47-assembly-release.tar.gz");
    }
    public static void main(String[] args) throws Exception {
//        String fileUrl="http://maven.kingstarfintech.com/repository/BNK/com/kingstar/ceres/bond-business-hsbc/2.0.8B47/bond-business-hsbc-2.0.8B47.jar";
//        String fileName="bond-business-hsbc-2.0.8B47.jar";
//        downloadFile(fileUrl,dir+fileName);
//        String dir="/home/liulei/Downloads/";
//        downloadFromUrl(fileUrl,dir);
        for (String name : list) {
            String fileName= null;
            String url= null;
            if (business.equals(name)){
                fileName = String.format(map.get("buName"),name,version);
                url = String.format(map.get("buUrl"),uri,name,version,fileName);
            }else if(ui.equals(name)) {
                fileName = String.format(map.get("uiName"),name,version);
                url = String.format(map.get("url"),uri,name,version,fileName);
            }else {
                fileName = String.format(map.get("fileName"),name,version);
                url = String.format(map.get("url"),uri,name,version,fileName);
            }
            System.out.println(fileName);
            System.out.println(url);
            downloadFile(url,dir+fileName);
        }
//        for (Map.Entry<String, String> entry : map.entrySet()) {
//            downloadFile(entry.getValue(),dir+entry.getKey());
//        }
    }
       public static void downloadFile(String fileUrl,String fileLocal) throws Exception {
              URL url = new URL(fileUrl);
            HttpURLConnection urlCon = (HttpURLConnection) url.openConnection();
               urlCon.setConnectTimeout(6000);
               urlCon.setReadTimeout(6000);
                int code = urlCon.getResponseCode();
                if (code != HttpURLConnection.HTTP_OK) {
            throw new Exception("文件读取失败");
            }

        //读文件流
        DataInputStream in = new DataInputStream(urlCon.getInputStream());
        DataOutputStream out = new DataOutputStream(new FileOutputStream(fileLocal));
        byte[] buffer = new byte[2048];
        int count = 0;
        while ((count = in.read(buffer)) > 0) {
            out.write(buffer, 0, count);
            }
        out.close();
        in.close();
        }

    public static String downloadFromUrl(String url,String dir) {

        try {
            URL httpurl = new URL(url);
//            String fileName = getFileNameFromUrl(url);
            String fileName = "bond-business-hsbc-2.0.8B47.jar";
            System.out.println(fileName);
            File f = new File(dir + fileName);
            FileUtils.copyURLToFile(httpurl, f);
        } catch (Exception e) {
            e.printStackTrace();
            return "Fault!";
        }
        return "Successful!";
    }

    public static String getFileNameFromUrl(String url){
        String name = new Long(System.currentTimeMillis()).toString() + ".jar";
        int index = url.lastIndexOf("/");
        if(index > 0){
            name = url.substring(index + 1);
            if(name.trim().length()>0){
                return name;
            }
        }
        return name;
    }
}
